package action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;

import dao.FriendshipDAO;
import dao.UserDAO;

import bean.Friendship;
import bean.FriendshipId;
import bean.User;

public class UserAction extends DispatchAction {

	public ActionForward view(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		SimpleDateFormat format =
            new SimpleDateFormat("MMM dd yyyy");
		UserDAO userDAO=new UserDAO();
		int userid=0;
		
		if(request.getParameter("viewuserid")==null)
			userid=((User)request.getSession(true).getAttribute("user")).getUId();
		else
			userid=Integer.parseInt( request.getParameter("viewuserid") );
		
		User user=userDAO.findById(userid);
		StringBuffer responseXML=new StringBuffer();
		
		responseXML.append("<user>");
		responseXML.append("<userfname>");
		responseXML.append(user.getUserfname());
		responseXML.append("</userfname>");
		responseXML.append("<userlname>");
		responseXML.append(user.getUserlname());
		responseXML.append("</userlname>");
		responseXML.append("<userEmail>");
		responseXML.append(user.getEmail());
		responseXML.append("</userEmail>");
		responseXML.append("<password>");
		responseXML.append(user.getPassword());
		responseXML.append("</password>");
		responseXML.append("<location>");
		responseXML.append(user.getUserlocation());
		responseXML.append("</location>");
		responseXML.append("<birthyear>");
		responseXML.append(user.getDob().getYear());
		responseXML.append("</birthyear>");
		responseXML.append("<birthmonth>");
		responseXML.append(user.getDob().getMonth()+1);
		responseXML.append("</birthmonth>");
		responseXML.append("<birthday>");
		responseXML.append(user.getDob().getDay());
		responseXML.append("</birthday>");
		responseXML.append("</user>");
		
		System.out.println(responseXML.toString());
		response.setContentType("text/xml");
		try {
			ServletOutputStream out=response.getOutputStream();
			out.print(responseXML.toString());			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public ActionForward update(
			ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		
		User oldUser=(User)request.getSession(true).getAttribute("user");
		String userFName=request.getParameter("userFName").trim();
		String userLName=request.getParameter("userLName").trim();
		String email=request.getParameter("email").trim();
		int gender=Integer.parseInt(request.getParameter("gender").trim());
		String password=request.getParameter("password").trim();
		int birth_year=Integer.parseInt(request.getParameter("year").trim());
		int birth_month=Integer.parseInt(request.getParameter("month").trim());
		int birth_day=Integer.parseInt(request.getParameter("day").trim());
		String location=request.getParameter("location").trim();
		
		UserDAO userDAO=new UserDAO();
		User newUser=userDAO.findById(oldUser.getUId());
		
		if(!newUser.getUserfname().equals(userFName)){
			newUser.setUserfname(userFName);			
		}
		
		if(!newUser.getUserlname().equals(userLName)){
			newUser.setUserlname(userLName);
		}
		
		if(!newUser.getPassword().equals(password)){
			newUser.setPassword(password);
		}
		
		if(!newUser.getEmail().equals(email)){
			newUser.setEmail(email);
		}
		
		if(!newUser.getGender().equals((byte)gender)){
			newUser.setGender((byte)gender);
		}
		
		int tempYear=newUser.getDob().getYear();
		int tempMonth=newUser.getDob().getMonth();
		int tempDay=newUser.getDob().getDay();
		
		if(tempMonth!=(birth_month-1)){
			tempMonth=birth_month;			
		}
		
		if(tempYear!=birth_year){
			tempYear=birth_year;			
		}
		
		if(tempDay!=birth_day){
			tempDay=birth_day;			
		}
		Date tempDate=new Date();
		tempDate.setYear(tempYear);
		tempDate.setMonth(tempMonth);
		tempDate.setDate(tempDay);
		newUser.setDob(tempDate);
		
		if(newUser.getUserlocation()==null||!newUser.getUserlocation().equals(location)){
			newUser.setUserlocation(location);
		}
		
		userDAO.merge(newUser);
		
		return mapping.findForward("success");
	}
}